const api = require('../config/api.js');
const util = require('../utils/util.js');
function wxpay( money, orderId, redirectUrl,userid) {
  
  util.request(api.WxPay, {
    'tradeno': orderId,
    'fee': money,
    'userid': userid
  }, 'POST').then(function (res) {
    console.log(res);
    if (res.code == 0) {
      if(money!='0.00'){
        // 发起支付
        wx.requestPayment({
          timeStamp: res.data.timeStamp + '',
          nonceStr: res.data.nonceStr + '',
          package: res.data.package,
          signType: 'MD5',
          paySign: res.data.paySign,
          fail: function (aaa) {
            console.log(aaa);
            wx.showToast({ title: '支付失败:' })
          },
          success: function (e) {
            console.log(e);

            wx.showToast({ title: '支付成功' })
            wx.redirectTo({
             // url: redirectUrl
            });
          }
        })
        wx.navigateBack({
          delta: 2
        })
      }
      else{
        wx.navigateBack({
          delta: 2
        })
        wx.showToast({ title: '订单成功成功' })
      }

   
    }
    else if (res.code == 400) {

    } else {
      util.showErrorToast("数据获取失败" + res.code);
    }
  });

}

module.exports = {
  wxpay: wxpay
}
